package com.taxi.driver.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;

import com.mikepenz.iconics.IconicsDrawable;
import com.taxi.driver.R;


/**
 * 可以设置TextView的drawable大小
 */
public class TextViewPlus extends TextView {

    //获取对应的属性值 Android框架自带的属性 attr
    int pressed = android.R.attr.state_pressed;
    int selected = android.R.attr.state_selected;


    public TextViewPlus(Context context) {
        super(context);
    }

    public TextViewPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    /**
     * 初始化读取参数
     */
    @SuppressLint("Recycle")
    private void init(Context context, AttributeSet attrs, int defStyle) {
        // TypeArray中含有我们需要使用的参数
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AllDerectionIcon, defStyle, 0);
        if (a == null) return;
        // 获取各个方向的图片，按照：左-上-右-下 的顺序存于数组中
        Drawable[] drawables = getCompoundDrawables();

        // 左图
        String leftIcon = a.getString(R.styleable.AllDerectionIcon_left_icon);
        if (!TextUtils.isEmpty(leftIcon)) {
            int color = a.getColor(R.styleable.AllDerectionIcon_left_color, 0);
            int size = a.getDimensionPixelSize(R.styleable.AllDerectionIcon_left_size, -1);
            drawables[0] = getDrawable(context, leftIcon, color, size);
        }
        // 上图
        String topIcon = a.getString(R.styleable.AllDerectionIcon_top_icon);
        if (!TextUtils.isEmpty(topIcon)) {
            int color = a.getColor(R.styleable.AllDerectionIcon_top_color, 0);
            int size = a.getDimensionPixelSize(R.styleable.AllDerectionIcon_top_size, -1);
            drawables[1] = getDrawable(context, topIcon, color, size);
        }
        // 右图
        String rightIcon = a.getString(R.styleable.AllDerectionIcon_right_icon);
        if (!TextUtils.isEmpty(rightIcon)) {
            int color = a.getColor(R.styleable.AllDerectionIcon_right_color, 0);
            int size = a.getDimensionPixelSize(R.styleable.AllDerectionIcon_right_size, -1);
            drawables[2] = getDrawable(context, rightIcon, color, size);
        }
        // 下图
        String bottomIcon = a.getString(R.styleable.AllDerectionIcon_bottom_icon);
        if (!TextUtils.isEmpty(bottomIcon)) {
            int color = a.getColor(R.styleable.AllDerectionIcon_bottom_color, 0);
            int size = a.getDimensionPixelSize(R.styleable.AllDerectionIcon_bottom_size, -1);
            drawables[3] = getDrawable(context, bottomIcon, color, size);
        }
        a.recycle();

        // 将图片放回到TextView中
        setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
    }

    private Drawable getDrawable(Context context, String icon, int color, int size) {
        IconicsDrawable drawable = new IconicsDrawable(context, icon);
        if (color != 0) drawable.color(color);
        if (size != -1) drawable.sizePx(size);
        return drawable;
    }

}